# Adogs
# 时间: 2021/8/22 18:59
import enum

import pygame

from dialog.actor_dialog import HongJunDialog
from dialog.tunnel_dialog import TunnelTask5BeginDialog, TunnelTask5EndDialog, TunnelTaskFailDialog
from scene.task import BaseTask, TaskStatus


class WayToWinTaskStatus(enum.IntEnum):
    finding = 0
    found = 1


class WayToWinTask(BaseTask):
    """
    任务五:解放村子
    """

    def __init__(self, xiao_tie, hong_jun, font):
        self.xiao_tie = xiao_tie
        self.hong_jun = hong_jun
        self.status = WayToWinTaskStatus.finding

        self.hong_jun_dialog = HongJunDialog(self.hong_jun, font)

        self.start_dialog = TunnelTask5BeginDialog(font)
        self.win_dialog = TunnelTask5EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)
        super(WayToWinTask, self).__init__(self.start_dialog, self.win_dialog, self.fail_dialog)

    def draw_task(self, surface, view_x, view_y):
        self.hong_jun.draw(surface, view_x + 20, view_y)

        if self.status == WayToWinTaskStatus.found:
            self.hong_jun_dialog.draw(surface, view_x, view_y)
            if self.hong_jun_dialog.is_finish():
                self.task_status = TaskStatus.win_dialog

    def __do_collide_actor(self, actor):
        """
        人物碰撞判断函数
        :param actor: 判断碰撞的人物
        :return: 无返回值
        """
        temp_rect = self.xiao_tie.rect
        self.xiao_tie.rect = pygame.Rect(self.xiao_tie.pos_x + 5,
                                         self.xiao_tie.pos_y + 15, 20, 60)
        is_collide = pygame.sprite.collide_rect(self.xiao_tie, actor)
        self.xiao_tie.rect = temp_rect
        if is_collide:
            return True

    def do_self_task(self, key_down, key_list):
        if key_list[pygame.K_z]:
            self.task_status = TaskStatus.win_dialog

        if self.__do_collide_actor(self.hong_jun):
            self.status = WayToWinTaskStatus.found
            if key_down and key_list[pygame.K_SPACE]:
                self.hong_jun_dialog.next_text()
            if self.hong_jun_dialog.is_finish():
                self.task_status = TaskStatus.win_dialog

